<?php

namespace App\Http\v1;

use App\Exceptions\v1\BaseException;

class ResponseFactory
{
    public static function created($data = null)
    {
        $r = [
            'version'       => '1',
            'http_status'   => '200',
        ];
        if ($data !== null) {
            $r['data'] = $data;
        }
        return response()->json($r, 201);
    }

    public static function deleted($data = null)
    {
        $r = [
            'version'       => '1',
            'http_status'   => '200',
        ];
        if ($data !== null) {
            $r['data'] = $data;
        }
        return response()->json($r, 204);
    }

    public static function success($data = null)
    {
        $r = [
            'version'       => '1',
            'http_status'   => '200',
        ];
        if ($data !== null) {
            $r['data'] = $data;
        }
        return response()->json($r, 200);
    }

    public static function failure(BaseException $e)
    {
        return response()->json([
            'version'       => '1',
            'http_status'   => (string)$e->getHttpStatus(),
            'code'          => (string)$e->getCode(),
            'message'       => $e->getMessage(),
            'external'      => $e->getExternalMessage(),
        ], $e->getHttpStatus());
    }
}

